package com.eyas.core.annotation.check;

import com.eyas.core.annotation.ColumnType;
import com.eyas.core.annotation.JdbcTemplateScanException;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.sql.Clob;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @Auther: 王龙龙
 * @Date: 2018/11/30 10:10
 * @Description:
 */
public class CheckAnnotationUtils {

    /**
     * 注解是否存在
     * @param annotatedElement
     * @param annotationClass
     * @return
     */
    public static boolean isAnnotationPersistent(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationClass) {
        if(AnnotationUtils.findAnnotation(annotatedElement,annotationClass)==null)
            return false;
        else
            return true;
    }

    /**
     * 注解是否同时存在
     * @param annotatedElement
     * @param annotationClass
     * @return
     */
    public static boolean isAnnotationsPersistent(AnnotatedElement annotatedElement, Class<? extends Annotation>[] annotationClass) {
        for(Class clazz:annotationClass) {
            if(AnnotationUtils.findAnnotation(annotatedElement,clazz)==null)
                return false;
        }
        return true;
    }


    /**
     * 存在某个注解的数量
     * @param annotatedElements
     * @param annotationClass
     * @return
     */
    public static int isAnnotationsExists(AnnotatedElement[] annotatedElements, Class<? extends Annotation> annotationClass) {
        int $1 = 0;
        if(annotatedElements==null)
            return $1;
        for(AnnotatedElement annotatedElement:annotatedElements) {
            if(isAnnotationPersistent(annotatedElement,annotationClass))
                $1++;
        }
        return $1;
    }


    /**
     * 校验model的属性类型是否能和注解的columnType对应
     * @param field
     * @param columnType
     */
    public static void validType(Class clazz,Field field, ColumnType columnType) {
        switch (columnType) {
            case SHORT:
                if(field.getType()!=short.class&&field.getType()!=Short.class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not short");
                }
                break;
            case INT:
                if(field.getType()!=int.class&&field.getType()!=Integer.class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not int");
                }
                break;
            case BIGINT:
                if(field.getType()!=long.class&&field.getType()!=Long.class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not long");
                }
                break;
            case BYTES:
                if(field.getType()!=byte[].class&&field.getType()!=Byte[].class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not byte[]");
                }
                break;
            case FLOAT:
                if(field.getType()!=float.class&&field.getType()!=Float.class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not float");
                }
                break;
            case DOUBLE:
                if(field.getType()!=double.class&&field.getType()!=Double.class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not double");
                }
                break;
            case BOOLEAN:
                if(field.getType()!=boolean.class&&field.getType()!=Boolean.class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not boolean");
                }
                break;
            case CHAR:
                if(field.getType()!=String.class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not String");
                }
                break;
            case STRING:
                if(field.getType()!=String.class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not String");
                }
                break;
            case TEXT:
                if(field.getType()!= Clob.class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not String or Clob");
                }
                break;
            case DATE:
                if(field.getType()!=Date.class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not Date");
                }
                break;
            case TIMESTAMP:
                if(field.getType()!=Timestamp.class) {
                    throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type is not Timestamp");
                }
                break;
            default:
                throw new JdbcTemplateScanException(clazz.getName()+"."+field.getName()+"'s type ["+columnType+"] is unknown");
        }
    }

}
